home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 203_01 / yam10.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-01-01  |  8.4 KB  |  387 lines

  1. /*
  2. $title ('yam10.c: file display, unsqueeze')
  3. $date (15 AUG 85)
  4. */
  5. /*
  6.  * File unsqueezer module from Richard Greenlaw's USQ
  7.  *  and other vital functions
  8. */
  9. #include "yam.h"
  10.  
  11. /* *** Stuff for first translation module *** */
  12. #define USDLE 0x90
  13. /* *** Stuff for second translation module *** */
  14. #define SPEOF 256    /* special endfile token */
  15. #define LARGE 30000
  16. int bpos;    /* last bit position read */
  17. int curin;    /* last byte value read */
  18. /* Variables associated with repetition decoding */
  19. int repct;    /*Number of times to return value*/
  20. int value;    /*current byte value or EOF */
  21.  
  22. /****************************************************************************
  23. FUNCTION:
  24.     unsqueeze a squeezed file to the crt.
  25.  
  26. CALLING PARAMETERS:
  27.     none
  28. ===========================================================================*/
  29. unsqueeze()
  30. {
  31.     int i, c;
  32.     int numnodes;        /* size of decoding tree */
  33.  
  34.     /* Initialization */
  35.     repct = 0;
  36.     bpos = 99;    /* force initial read */
  37.  
  38.     /* Process rest of header (SQMAGIC already read) */
  39.     getw(fin);    /* ignore checksum ... */
  40.  
  41.     /* List current and original file names */
  42.     fprintf(stderr, "%s -> ", Tname);
  43.     while(c = getc(fin))
  44.         putc(c, stderr);
  45.     putc('\n', stderr);
  46.  
  47.     numnodes = getw(fin);
  48.     if(numnodes < 0 || numnodes >= NUMVALS)
  49.     {
  50.         printf(stderr, "%s has invalid decode tree size\n", Tname);
  51.         return ERROR;
  52.     }
  53.  
  54.     /* Initialize for possible empty tree (SPEOF only) */
  55.     Utility.dnode[0].children[0] = -(SPEOF + 1);
  56.     Utility.dnode[0].children[1] = -(SPEOF + 1);
  57.  
  58.     /* Get decoding tree from file */
  59.     for(i = 0; i < numnodes; ++i)
  60.     {
  61.         Utility.dnode[i].children[0] = getw(fin);
  62.         Utility.dnode[i].children[1] = getw(fin);
  63.     }
  64.  
  65.     while((c = getcr()) != CPMEOF)
  66.     {
  67.         if( !(c=putcty(c)))
  68.             continue;
  69.         if(c==003 || c==CAN || c==013)
  70.         {
  71.             return c;
  72.         }
  73.     }
  74.     return CPMEOF;
  75. } /* unsqueeze */
  76.  
  77.  
  78. /****************************************************************************
  79. FUNCTION:
  80.     Get bytes with decoding - this decodes repetition,
  81.     calls getuhuff to decode file stream into byte
  82.     level code with only repetition encoding.
  83.     The code is simple passing through of bytes except
  84.     that USDLE is encoded as USDLE-zero and other values
  85.     repeated more than twice are encoded as value-USDLE-count.
  86.  
  87. CALLING PARAMETERS:
  88.     none
  89. ===========================================================================*/
  90. int getcr()
  91. {
  92.     int c;
  93.  
  94.     if(repct > 0)
  95.     {
  96.         /* Expanding a repeated char */
  97.         --repct;
  98.         return value;
  99.     }
  100.     else
  101.     {
  102.         /* Nothing unusual */
  103.         if((c = getuhuff()) != USDLE)
  104.         {
  105.             /* It's not the special delimiter */
  106.             value = c;
  107.             if(value == CPMEOF)
  108.                 repct = LARGE;
  109.             return value;
  110.         }
  111.         else
  112.         {
  113.             /* Special token */
  114.             if((repct = getuhuff()) == 0)
  115.                 /* USDLE, zero represents USDLE */
  116.                 return USDLE;
  117.             else
  118.             {
  119.                 /* Begin expanding repetition */
  120.                 repct -= 2;    /* 2nd time */
  121.                 return value;
  122.             }
  123.         }
  124.     }
  125. }
  126.  
  127. /****************************************************************************
  128. FUNCTION:
  129.     Decode file stream into a byte level code with only
  130.     repetition encoding remaining.
  131.  
  132. CALLING PARAMETERS:
  133.     none
  134. ===========================================================================*/
  135. int
  136. getuhuff()
  137. {
  138.     int i;
  139.  
  140.     /* Follow bit stream in tree to a leaf*/
  141.     i = 0;    /* Start at root of tree */
  142.     do
  143.     {
  144.         if(++bpos > 7)
  145.         {
  146.             if((curin = getc(fin)) == ERROR)
  147.                 return ERROR;
  148.             bpos = 0;
  149.             /* move a level deeper in tree */
  150.             i = Utility.dnode[i].children[1 & curin];
  151.         } else
  152.             i = Utility.dnode[i].children[1 & (curin >>= 1)];
  153.     } while(i >= 0);
  154.  
  155.     /* Decode fake node index to original data value */
  156.     i = -(i + 1);
  157.     /* Decode special endfile token to normal EOF */
  158.     i = (i == SPEOF) ? CPMEOF : i;
  159.     return i;
  160. }
  161.  
  162.  
  163. /****************************************************************************
  164. FUNCTION:
  165.     put char to console.  Characters are checked for rxnono and not output
  166.     if in this string.  Tabs are expanded and ^S is tested for.
  167.  
  168. CALLING PARAMETERS:
  169.     c:
  170.         character to output to console.
  171. ===========================================================================*/
  172. char putcty(c)
  173. char c;
  174. {
  175.     if(index(c, rxnono))
  176.         return FALSE;
  177.     while (!COREADY)
  178.         ;
  179.  
  180.     switch(c)
  181.     {
  182.             /* expand tabs */
  183.         case '\t':
  184.             c=' ';        
  185.             while (++Ttycol % tabstop)
  186.                 CONOUT(' ');        
  187.             break;
  188.         case '\r':
  189.         case '\n':
  190.             Ttycol=0;
  191.             break;
  192.         default:
  193.             ++Ttycol;
  194.     }
  195.     CONOUT(c);
  196.  
  197.     if(CIREADY)
  198.     {
  199.         c = (char)CICHAR;
  200.             /* pause on ^S */
  201.         if (c==xoff)
  202.         {
  203.             while (!CIREADY)
  204.                 ;
  205.             return FALSE;
  206.         }
  207.         else
  208.             return c;
  209.     }
  210.     else
  211.         return FALSE;
  212. } /* putcty */
  213.  
  214.  
  215. /****************************************************************************
  216. FUNCTION:
  217.     get char from console.
  218.  
  219. CALLING PARAMETERS:
  220.     none
  221. ===========================================================================*/
  222. char getcty()
  223. {
  224.     while (!CIREADY)
  225.         ;
  226.     return (char)(CICHAR&KBMASK);
  227. } /* getcty */
  228.  
  229.  
  230. /****************************************************************************
  231. FUNCTION:
  232.     display a file to the crt.  This routine will open and close the file.
  233.     if USQ is set, squeezed files may be typed.
  234.  
  235. CALLING PARAMETERS:
  236.     f_buf:
  237.         pointer to a structure containing information about file.  This
  238.         is not used, but supplied by expand.
  239.     *pathname:
  240.         pointer to directory path name
  241. ===========================================================================*/
  242. listfile(f_buf,pathname)
  243. struct find_buf *f_buf;
  244. char *pathname;
  245. {
  246.     int c;
  247.  
  248.         /* reset tab stop counter */
  249.     Ttycol = 0;
  250.     closetx(TRUE);
  251.  
  252.         /* open file. open will not compute or display file size */
  253.     if(opentx(NULL,NULL,pathname)==ERROR)
  254.         return ERROR;
  255. #ifdef XMODEM
  256.     sendbyte(022);        /* bracket with ^R and ^T for squelch */
  257. #endif
  258. #ifdef USQ
  259.     c = getw(fin);
  260.     if(c==SQMAGIC)
  261.         c=unsqueeze();
  262.  
  263.         /* wasn't a squeezed file, display the first 2 chars fetched */
  264.     putcty(c);
  265.     putcty(c>>8);
  266. #endif
  267.     while( ((c=getc(fin)) != EOF) && (c != CPMEOF) )
  268.     {
  269.             /* returns false if no key pressed */
  270.         if( !(c=putcty(c)&0x7f))
  271.             continue;
  272.  
  273.             /* ^C=abort, ^X=next file */
  274.         if(c==ETX || c==CAN)
  275.                 break;
  276.     }
  277.     if(c != CPMEOF)
  278.         putchar('\n');
  279.  
  280.     /* record complete xmsn if terminated by (CPM)EOF */
  281.     closetx(c != CPMEOF);
  282. #ifdef XMODEM
  283.     sendbyte(024);    /* squelch in case user downloading */
  284. #endif        
  285.     /* cancel rest of files if ^C */
  286.     if(c==ETX)
  287.         return ERROR;
  288.     else
  289.         return OK;
  290. } /* listfile */
  291.  
  292.  
  293. /****************************************************************************
  294. FUNCTION:
  295.     get a word from a buffer input file by 2 consective calls to getc
  296.     This call does not exist in lattice C.
  297.  
  298. CALLING PARAMETERS:
  299.     fin:
  300.         pointer to FILE variable.
  301. ===========================================================================*/
  302. getw(fp)
  303. FILE *fp;
  304. {
  305.     union
  306.     {
  307.         char cb[2];
  308.         int cw;
  309.     } cword;
  310.  
  311.     cword.cb[0] = (char)getc(fp);
  312.     cword.cb[1] = (char)getc(fp);
  313.     return cword.cw;
  314. } /* getw */
  315.  
  316.  
  317. /****************************************************************************
  318. FUNCTION:
  319.     fill buf with count chars padding with ^Z for CPM
  320.  
  321. CALLING PARAMETERS:
  322.     buf:
  323.         pointer to buffer
  324.     count:
  325.         number of bytes to pad buffer with
  326. ===========================================================================*/
  327. filbuf(buf, count)
  328. char *buf;
  329. {
  330.     register c, m;
  331.     m=count;
  332.     while((c=getc(fin))!=EOF)
  333.     {
  334.         *buf++ =c;
  335.         if(--m == 0)
  336.             break;
  337.     }
  338.     if(m==count)
  339.         return 0;
  340.     else
  341.         while(--m>=0)
  342.             *buf++ = 032;
  343.     return count;
  344. } /* filbuf */
  345.  
  346.  
  347. /****************************************************************************
  348. FUNCTION:
  349.     compare two strings of characters.  If equal return true, else false
  350.  
  351. CALLING PARAMETERS:
  352.     s:
  353.         pointer to first string
  354.     p:
  355.         pointer to second string
  356. ===========================================================================*/
  357. cmdeq(s,p)
  358. char *s, *p;
  359. {
  360.     while(*p)
  361.         if(*s++ != *p++)
  362.             return FALSE;
  363.     return TRUE;
  364. } /* cmdeq */
  365.  
  366.  
  367. /****************************************************************************
  368. FUNCTION:
  369.     local tell-it-like-it-is string write function with no kbd check
  370.  
  371. CALLING PARAMETERS:
  372.     *s:
  373.         pointer to string to place on screen
  374. ===========================================================================*/
  375. lputs(s)
  376. char *s;
  377. {
  378.     while(*s)
  379.     {
  380. #ifdef COREADY
  381.         while (!COREADY)
  382.             ;
  383. #endif
  384.         CONOUT(*s++);
  385.     }
  386. } /* lputs */
  387.